package com.sec.android.app.myfiles.module.suggestion.cmd;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;

import com.sec.android.app.myfiles.log.Log;
import com.sec.android.app.myfiles.module.abstraction.FileRecord;
import com.sec.android.app.myfiles.provider.DbTableInfo;
import com.sec.android.app.myfiles.util.SemFwWrapper;
import com.sec.android.app.myfiles.util.StorageMonitor;

import org.puremvc.java.multicore.interfaces.INotification;
import org.puremvc.java.multicore.patterns.command.SimpleCommand;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;

public class AddSuggestionCmd extends SimpleCommand {
    @Override
    public void execute(INotification notification) {
        Object[] params = (Object[]) notification.getBody();
        final Context context = (Context) params[0];
        final FileRecord record = (FileRecord) params[1];
        String currentPath = record.getFullPath();

        ArrayList<String> paths = new ArrayList<>();
        paths.add(currentPath);
        addSuggestionInfo(context, record, paths);
    }

    public void addSuggestionInfo(Context context, FileRecord record, final ArrayList<String> fullPaths) {
        FileRecord.StorageType storageType = record.getStorageType();
        ContentResolver cr = context.getContentResolver();
        DbTableInfo tableInfo = DbTableInfo.getInstance(FileRecord.StorageType.Suggestion);
        if (storageType == FileRecord.StorageType.FolderTree) {
            storageType = FileRecord.StorageType.Local;
        }

        for (String path : fullPaths) {
            long date = 0;
            File file = SemFwWrapper.file(path);
            if (file.exists()) {
                date = file.lastModified();
            }

            if (tableInfo != null) {
                context.getContentResolver().delete(Uri.parse(tableInfo.getUri()), null, null);

                // insert
                ContentValues values = new ContentValues();
                values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.DATE), date);
                values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH), path);
                values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.STORAGE_TYPE), storageType.ordinal());
                values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.NAME), StorageMonitor.getStorageDisplayName(context, path));
                Uri uri = Uri.parse(tableInfo.getUri());
                cr.insert(uri, values);

                /*  concept에 따라 추가 필요
                int id = FileUtils.isAddedToSuggestion(context, path);
                if (id > 0) {
                    // update only last modified time
                    ContentValues values = new ContentValues();
                    values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.DATE), date);
                    Uri uri = Uri.parse(tableInfo.getUri());
                    cr.update(uri, values, "_id=" + id, null);
                } else {
                    // insert
                    ContentValues values = new ContentValues();
                    values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.DATE), date);
                    values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.PATH), path);
                    values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.STORAGE_TYPE), storageType.ordinal());
                    values.put(tableInfo.getColumnName(DbTableInfo.COLUMN_ID.NAME), FileUtils.getShortcutDisplayName(context, path));
                    Uri uri = Uri.parse(tableInfo.getUri());
                    cr.insert(uri, values);
                }
                */
            }
        }

        test();
    }


    public void test() {
        Calendar currentCalendar = Calendar.getInstance();
        Calendar aWeekAgo = Calendar.getInstance();
        aWeekAgo.add(Calendar.DATE, -7);
        long diffTimeMillis = currentCalendar.getTimeInMillis() - aWeekAgo.getTimeInMillis();

        Log.e(this, "Test : current time is: " + currentCalendar.getTimeInMillis());
        Log.e(this, "Test : aWeekAgo's time is: " + aWeekAgo.getTimeInMillis());

        int year = aWeekAgo.get(aWeekAgo.YEAR);
        int month = aWeekAgo.get(aWeekAgo.MONTH) + 1;
        int day = aWeekAgo.get(aWeekAgo.DAY_OF_MONTH);

        Log.e(this, "Test : aWeekAgo's year : " + year);
        Log.e(this, "Test : aWeekAgo's month : " + month);
        Log.e(this, "Test : aWeekAgo's day : " + day);

        /*
        long now = System.currentTimeMillis();
        Date date = new Date(now);
        date.setDate(date.getDate()-7);
*/
        /*
        Calendar currentCalendar = Calendar.getInstance();
      Calendar createdCalendar = Calendar.getInstance();
      createdCalendar.setTime(date);
      long diffTimeMillis = currentCalendar.getTimeInMillis() - createdCalendar.getTimeInMillis();

      String printStr = "";
      if (diffTimeMillis / (24 * 60 * 60 * 1000) > 0)
         printStr = diffTimeMillis / (24 * 60 * 60 * 1000) + " " + "일 전";
      else if (diffTimeMillis / (60 * 60 * 1000) > 0)
         printStr = diffTimeMillis / (60 * 60 * 1000) + " " + "시간 전";
      else if (diffTimeMillis / (60 * 1000) > 0)
         printStr = diffTimeMillis / (60 * 1000) + " " + "분 전";
      else if (diffTimeMillis / 1000 > 0)
         printStr = diffTimeMillis / 1000 + " " + "초 전";
         */
    }
}
